<?php

/**
 * Created by PhpStorm.
 * User: trunganh
 * Date: 4/28/14
 * Time: 16:59
 */
class HappyController extends Controller
{

    public $uID;
    public $userName;
    public $userLocation;
    public $userEmail;
    public $userSex;
    public $params = array(
        'scope' => 'read_stream,read_friendlists, user_photos,email,user_location',
        'redirect_uri' => 'https://mobay.vn/happy/happy',
        'next' => 'https://www.facebook.com/zik.oops'
    );


    public function actionIndex()
    {


//        echo '<pre>';
//        var_dump($numberOfPost);
//        die();
//        echo '/<pre>';


        $fb = Fbapi::getFBAPI();
        $user_id = $fb->getUser();
        $login_url = null;
        $flag = null;


        if ($user_id) {
            $user_profile = $fb->api('/me', 'GET');

//            $dump = "https://graph.facebook.com/".$user_profile['id']."/picture?width=150&height=150";
//


            $save_user = new TblUser();

            if ($user_profile['email']) {
                $save_user->email = $user_profile['email'];
            }
            if ($user_profile['name']) {
                $save_user->name = $user_profile['name'];
            }
            if ($user_profile['location']) {
                $save_user->location = $user_profile['location']['name'];

            }
            if ($user_profile['username']) {
                $save_user->username = $user_profile['username'];
            }
            if ($user_profile['gender']) {
                $save_user->sex = $user_profile['gender'];
            }

            $query = "SELECT uid,name,sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me() ORDER BY rand()) LIMIT 20";
            $friends = $fb->api(array(
                'method' => 'fql.query',
                'query' => $query,
            ));


            $friends_id = array();
            $friends_name = array();
            for ($i = 0; $i < count($friends); $i++) {
                $friends_id[] = ($friends[$i]['uid']);
                $friends_name[] = ($friends[$i]['name']);
            }


            foreach ($friends_id as $f) {
                $response = $fb->api(
                    "/$f/picture",
                    "GET",
                    array(
                        'redirect' => false,
                        'height' => '100',
                        'type' => 'normal',
                        'width' => '100',
                    )
                );

                $profile_picture[] = $response['data']['url'];
            }


            $me_avatar = $fb->api(
                "/me/picture",
                "GET",
                array(
                    'redirect' => false,
                    'height' => '100',
                    'type' => 'normal',
                    'width' => '100',
                )
            );


            for ($i = 0; $i < 20; $i++) {
                $numberOfPost[] = rand(20, 100);
            }

            for ($i = 0; $i < 20; $i++) {
                $numberOfScore[] = rand(1, 100);
            }


            $flag = 1;
            $save_user->save();
            $this->render('index', array(
                'friend_id' => $friends_id,
                'flag' => $flag,
                'profile_picture' => $profile_picture,
                'friend_name' => $friends_name,
                'me_avatar' => $me_avatar,
                'me_name' => $user_profile['name'],
                'numberOfPost' => $numberOfPost,
                'numberOfScore' => $numberOfScore,

            ));

        } else {
            $flag = 0;
            $login_url = $fb->getLoginUrl($this->params);
            $this->render('index', array(
                'login_url' => $login_url,
                'flag' => $flag,
            ));

        }


    }

    public function actionLoginGoogle()
    {

        $this->render('google');
    }
//        echo "id: " . $user_profile['id']."</br>";
//        echo "Name: " . $user_profile['name']."</br>";
//        echo "locale: " . $user_profile['location']['name']."</br>";
//        echo "username: " . $user_profile['username']."</br>";
//        echo "email: " . $user_profile['email']."</br>";
//        echo "Sex: " . $user_profile['gender']."</br>";


} 